package nero.yncc.com.createart.utils;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.io.File;

import cn.finalteam.galleryfinal.GalleryFinal;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import nero.yncc.com.createart.R;

/**
 * Created by nero on 17-6-28.
 */

public class ImageUtils {

    public static void BindImage(Context context, ImageView imageView,String url,int big){
        try {
            switch (big){
                case 0:
                    Glide.with(context).load(url) .placeholder(R.mipmap.load_1) .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
                    break;
                case 1:
                    Glide.with(context).load(url) .placeholder(R.mipmap.load_1) .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
                    break;
                case 2:
                    Glide.with(context).load(url) .placeholder(R.mipmap.load_2) .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
                    break;
                case 3:
                    Glide.with(context).load(url) .placeholder(R.mipmap.load_3) .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
                    break;
                case 4:
                    Glide.with(context).load(url) .placeholder(R.mipmap.load_big) .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
                    break;
            }
        }catch (IllegalArgumentException e){}


    }

    public static  void BindImageScale(Context context, final ImageView imageView, final String imageUrl) {
        /**
         * 自适应宽度加载图片。保持图片的长宽比例不变，通过修改imageView的高度来完全显示图片。
         */
            Glide.with(context)
                    .load(imageUrl)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            if (imageView == null) {
                                return false;
                            }
                            if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
                                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                            }
                            ViewGroup.LayoutParams params = imageView.getLayoutParams();
                            int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
                            float scale = (float) vw / (float) resource.getIntrinsicWidth();
                            int vh = Math.round(resource.getIntrinsicHeight() * scale);
                            params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
                            imageView.setLayoutParams(params);
                            return false;
                        }
                    })
                    .placeholder(R.mipmap.maxbig)
                    .error(R.mipmap.maxbig)
                    .into(imageView);
    }

    public static void BindImage(Context context, ImageView imageView,String url,int big,int cor){
        switch (big){
            case 0:
                Glide.with(context).load(url) .placeholder(R.mipmap.load_1) .diskCacheStrategy(DiskCacheStrategy.SOURCE).bitmapTransform(new RoundedCornersTransformation(context, cor, 0, RoundedCornersTransformation.CornerType.ALL)).into(imageView);
                break;
            case 1:
                Glide.with(context).load(url) .placeholder(R.mipmap.load_1) .diskCacheStrategy(DiskCacheStrategy.SOURCE).bitmapTransform(new RoundedCornersTransformation(context, cor, 0, RoundedCornersTransformation.CornerType.ALL)).into(imageView);
                break;
            case 2:
                Glide.with(context).load(url) .placeholder(R.mipmap.load_2) .diskCacheStrategy(DiskCacheStrategy.SOURCE).bitmapTransform(new RoundedCornersTransformation(context, cor, 0, RoundedCornersTransformation.CornerType.ALL)).into(imageView);
                break;
            case 3:
                Glide.with(context).load(url) .placeholder(R.mipmap.load_3) .diskCacheStrategy(DiskCacheStrategy.SOURCE).bitmapTransform(new RoundedCornersTransformation(context, cor, 0, RoundedCornersTransformation.CornerType.ALL)).into(imageView);
                break;
            case 4:
                Glide.with(context).load(url) .placeholder(R.mipmap.load_big) .diskCacheStrategy(DiskCacheStrategy.SOURCE).bitmapTransform(new RoundedCornersTransformation(context, cor, 0, RoundedCornersTransformation.CornerType.ALL)).into(imageView);
                break;
        }

    }


    /**
     * 常规加载图片
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImageNormal(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(imageView);
    }
    public static void loadImageNormal(Context context, String url, int placeholder, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(placeholder)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(imageView);
    }
    public static void loadImageNormal(Context context, File file, int placeholder, ImageView imageView) {
        Glide.with(context)
                .load(file)
                .placeholder(placeholder)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(imageView);
    }

    /**
     * 加载圆形头像
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImageCircle(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
//                .placeholder(R.drawable.ic_head_default)
                .bitmapTransform(new CropCircleTransformation(context))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(imageView);
    }
    public static void loadImageCircle(Context context, String url, int placeholder, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .placeholder(placeholder)
                .bitmapTransform(new CropCircleTransformation(context))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(imageView);
    }

    /**
     * 加载矩形圆角图片
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImageRoundedCorners(Context context, String url, ImageView imageView,int cor) {
        Glide.with(context)
                .load(url)
                .bitmapTransform(new RoundedCornersTransformation(context, cor, 0, RoundedCornersTransformation.CornerType.ALL))
                .into(imageView);
    }
    //加载本地
    public static void loadImageRoundedCorners(Context context, int pic, ImageView imageView,int cor) {
        Glide.with(context)
                .load(pic)
                .bitmapTransform(new RoundedCornersTransformation(context, cor, 0, RoundedCornersTransformation.CornerType.ALL))
                .into(imageView);
    }

}
